package com.fishery.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.base.entity.GermchitOrder;
import com.fishery.base.entity.vo.BaseGermchitAmountVo;
import com.fishery.base.entity.vo.BaseGermchitVo;
import com.fishery.base.entity.vo.GermchitOrderShowVo;
import com.fishery.base.entity.vo.GermchitOrderVo;
import com.fishery.base.service.GermchitManagementService;
import com.fishery.base.service.GermchitOrderService;
import com.fishery.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * <p>
 * 基地种苗前端控制器
 * </p>
 *
 * @author Ghost
 * @since 2021-01-15
 */
@RestController
@RequestMapping("/base/germchit")
@CrossOrigin
@Api(tags = "基地种苗管理模块")
public class GermchitManagerController {


	@Autowired
	private GermchitManagementService germchitManagementService;

	@Autowired
	private GermchitOrderService germchitOrderService;


	@PostMapping("/purchase")
	@ApiOperation("基地购买种苗")
	public Result purchaseGermchit(@RequestBody @ApiParam(value = "种苗订单实体", required = true) GermchitOrderVo germchitOrderVo) {
		if (germchitOrderVo == null
				|| StringUtils.isEmpty(germchitOrderVo.getGermchitId())
				|| StringUtils.isEmpty(germchitOrderVo.getBaseId())
				|| germchitOrderVo.getPurchaseAmount() <= 0) {
			return Result.error("购买失败");
		}
		boolean ret = germchitManagementService.purchaseGermchit(germchitOrderVo);
		return ret ? Result.success("购买成功") : Result.error("购买失败");
	}


	@PutMapping("/{orderId}")
	@ApiOperation("通过订单ID修改种苗订单状态为已送达状态")
	public Result germchitArrival(@PathVariable(name = "orderId", required = true) @ApiParam(value = "订单ID", required = true) String orderId) {
		if (StringUtils.isEmpty(orderId)) {
			return Result.error("签收失败");
		}
		boolean ret = germchitOrderService.updateOrderStatus(orderId);
		return ret ? Result.success().data("签收成功") : Result.error("签收失败");
	}


	@GetMapping("/order/{baseId}/{size}/{current}")
	@ApiOperation("查询基地所有的种苗订单（带分页）")
	public Result getAllOrder(@PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地ID") String baseId,
							  @PathVariable(name = "size", required = true) @ApiParam(value = "尺寸", required = true) int size,
							  @PathVariable(name = "current", required = true) @ApiParam(value = "页码", required = true) int current) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		Page<GermchitOrderShowVo> page = germchitOrderService.pageOrders(baseId, size, current);
		return Result.success().data(page);
	}


	@GetMapping("/order/{orderId}")
	@ApiOperation("通过订单ID获取种苗订单信息")
	public Result gerOrderById(@PathVariable(name = "orderId", required = true) @ApiParam(value = "订单Id", required = true) String orderId) {
		if (StringUtils.isEmpty(orderId)) {
			return Result.error("查询失败");
		}
		return Result.success().data(germchitOrderService.getGermchitOrderShowVoById(orderId));
	}


	@DeleteMapping("/order/{orderId}")
	@ApiOperation("通过订单ID删除种苗订单")
	public Result deleteOrderById(@PathVariable(name = "orderId", required = true) @ApiParam(value = "订单Id", required = true) String orderId) {
		if (StringUtils.isEmpty(orderId)) {
			return Result.error("删除失败");
		}
		boolean ret = germchitOrderService.removeById(orderId);
		return ret ? Result.success("删除成功") : Result.error("删除失败");
	}


	@PutMapping("/order")
	@ApiOperation("修改种苗订单信息")
	public Result updateOrder(@RequestBody @ApiParam(value = "种苗订单实体") GermchitOrder order) {
		if (order == null || StringUtils.isEmpty(order.getId())) {
			return Result.error("修改失败");
		}
		boolean ret = germchitOrderService.updateById(order);
		return ret ? Result.success("修改成功") : Result.error("修改失败");
	}


	@ApiOperation("通过基地ID获取基地所有种苗信息")
	@GetMapping("/{baseId}")
	public Result getBaseGermchitInfo(@PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地ID") String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error();
		}
		List<BaseGermchitVo> baseGermchitVos = germchitManagementService.getAllGermchitInfo(baseId);
		return Result.success().data(baseGermchitVos);
	}

	@ApiOperation("通过基地ID获取基地所有种苗信息(带分页)")
	@GetMapping("/{baseId}/{size}/{current}")
	public Result getBaseGermchitInfoWithPage(@PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地ID") String baseId,
											  @PathVariable(name = "size", required = true) @ApiParam(required = true, value = "尺寸") int size,
											  @PathVariable(name = "current", required = true) @ApiParam(required = true, value = "页码") int current) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error();
		}
		Page<BaseGermchitVo> page = germchitManagementService.getAllGermchitInfoWithPage(baseId, size, current);
		return Result.success().data(page);
	}

	@GetMapping("/excel/{baseId}")
	@ApiOperation("将基地的种苗订单以Excel格式导出")
	public void getExcelOfGermchitOrders(HttpServletResponse response,
										 @PathVariable(name = "baseId", required = true) @ApiParam(required = true, value = "基地ID") String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return;
		}
		germchitOrderService.getGermchitOrdersDataByExcel(response, baseId);
	}

	@GetMapping("/order/sort/{baseId}")
	@ApiOperation("查询基地各种类型种苗的进货量、种苗的库存量、种苗的使用量、水产品产量（供领导仓调用）")
	public Result getAmountOfVariousGermchit(@PathVariable(name = "baseId", required = true) String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		List<BaseGermchitAmountVo> list = germchitManagementService.getAmountInfoOfGermchit(baseId);
		return Result.success().data(list);
	}

}
